class Solution {
public:
    int numberOfArithmeticSlices(vector<int>& nums) {
        int m=nums.size();
        if(m<3) return 0;
        vector<vector<int>> dp(m,vector<int>(m,0));
        int ret=0;
        unordered_map<long long,vector<int>> hash;
        for(int i=0;i<m;++i)
            hash[nums[i]].push_back(i);
        for(int j=2;j<m;++j)
        {
            for(int i=1;i<j;++i)
            {
                long long a=(long long)2*nums[i]-nums[j];
                if(hash.count(a))
                {
                    for(auto k:hash[a])
                    {
                        if(k<i)
                            dp[i][j]+=dp[k][i]+1;
                    }
                }
                ret+=dp[i][j];
            }
        }
        return ret;
    }
};
